<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addRole(1, '0')">添加权限</el-button>
        </div>
        <!-- 表格的数据有children节点 ,要渲染树形结构 添加 row-key="id"  -->
        <el-table :data="permissionList" border row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!--   二级权限的pid === 一级权限的id -->
              <el-button v-if="row.type === 1" size="small" type="text" @click="addRole(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="showRole(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 新增编辑的弹层 -->
        <el-dialog :visible="showDialog" :title="title" @close="closeDiaglog">
          <!-- 表单内容 -->
          <el-form v-loading="loading" label-width="100px">
            <el-form-item label="权限名称">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
                active-text="启用"
                inactive-text="禁用"
              />
            </el-form-item>
          </el-form>

          <template #footer>
            <div style="text-align: right;">
              <el-button @click="closeDiaglog">取消</el-button>
              <el-button type="primary" @click="clickSubmit">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqUpdatePermission, reqGetPermissionDetail, reqDelPermission, reqAddPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permissions',
  data() {
    return {
      permissionList: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 权限的启用和禁用状态 0表示关闭 1表示开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      title: '',
      loading: false
    }
  },
  // computed: {
  //   title() {
  //     return this.formData.id ? '编辑' : '新增'
  //   }
  // },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      // 树形数据格式处理
      this.permissionList = tranListToTreeData(data, '0')
    },
    // 删除权限
    delRole(id) {
      // 1. 确认消息框
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 发送请求
        await reqDelPermission(id)
        // 3. 提示用户
        this.$message.success('删除成功')
        // 4. 刷新页面
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加权限
    addRole(type, pid) {
      // 1. 展示对话框
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
      this.title = '添加权限'
    },
    closeDiaglog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 权限的启用和禁用状态 0表示关闭 1表示开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 编辑/添加
    async clickSubmit() {
      // 预校验
      if (this.formData.name === '' || this.formData.code === '') return
      if (this.formData.id) {
        // 编辑
        await reqUpdatePermission(this.formData)
      } else {
      // 新增
        await reqAddPermission(this.formData)
      }
      this.$message.success('操作成功')
      this.getPermissionList()
      this.closeDiaglog()
    },
    // 点击查看
    async showRole(id) {
      // 展示对话框
      this.showDialog = true
      this.loading = true
      this.title = '查看权限'
      //  拿着id查询
      const { data } = await reqGetPermissionDetail(id)
      // 将查询到的信息交给formData
      this.formData = data
      this.loading = false
    }

  }
}
</script>
